//
//  PPZBarViewController.m
//  PetParadise
//
//  Created by 陈无忌 on 8/22/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPZBarViewController.h"
#import "supportingHeadFiles.h"
#import "PPUserInfo.h"
#import <sys/utsname.h>
#import "PPCameraAuthorization.h"

@interface PPZBarViewController ()
@property (weak, nonatomic) IBOutlet UILabel *dataLabel;
@property (weak, nonatomic) IBOutlet ZBarReaderView *scanView;
@property (weak, nonatomic) IBOutlet UIView *scanAreaView;
@property (weak, nonatomic) PPSystemConfiguraton *config;

@end

@implementation PPZBarViewController
- (PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

NSString*
machineName()
{
    struct utsname systemInfo;
    uname(&systemInfo);
    
    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.scanView.readerDelegate = self;
//    NSString *deviceModel = machineName();
    @try {
        self.scanView.session.sessionPreset = AVCaptureSessionPreset1920x1080;
    }
    @catch (NSException *exception) {
        self.scanView.session.sessionPreset = AVCaptureSessionPresetMedium;
    }
    @finally {
        
    }
    
    
    if (IS_OS_7_OR_LATER) {
        [PPCameraAuthorization getCameraAuthorization];
        if (!self.config.isCameraValid) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请在iPhone的“设置-隐私-相机”选项中，允许程序访问你的相机" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
            [alert show];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
    
    
    
    [self.scanView.scanner setSymbology:0 config:ZBAR_CFG_ENABLE to:0];
    [self.scanView.scanner setSymbology:ZBAR_QRCODE config:ZBAR_CFG_ENABLE to:1];
    //self.scanView.scanCrop = [self.view convertRect:self.scanAreaView.frame toView:self.scanView];
    [self.scanView.scanner setSymbology:0 config:ZBAR_CFG_X_DENSITY to:1];
    [self.scanView.scanner setSymbology:0 config:ZBAR_CFG_Y_DENSITY to:1];
//    self.scanView.scanCrop = CGRectMake(0.125, 0, .75, 1);
//    CGRect frame = self.scanView.scanCrop;
    
    //NSLog(@"crop frame is %f, %f, %f, %f", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
//    ZBarReaderView *readerView = [[ZBarReaderView alloc]init];
//    readerView.frame = self.scanView.frame;
//    readerView.readerDelegate = self;
//    //关闭闪光灯
//    readerView.torchMode = 0;
//    //扫描区域
//    CGRect scanMaskRect = CGRectMake(60, 90, 200, 200);
//    UIView *scanView = [[UIView alloc] initWithFrame:CGRectMake(60, 90, 200, 200)];
//    [scanView setBackgroundColor:[UIColor colorWithRed:102 green:102 blue:102 alpha:0.5]];
//    [readerView addSubview:scanView];
//    //处理模拟器
//    if (TARGET_IPHONE_SIMULATOR) {
//        ZBarCameraSimulator *cameraSimulator
//        = [[ZBarCameraSimulator alloc]initWithViewController:self];
//        cameraSimulator.readerView = readerView;
//    }
//    [self.scanView addSubview:readerView];
//    //扫描区域计算
//    readerView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readerView.bounds];
//    
//    [readerView start];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [self.scanView start];
    self.scanAreaView.layer.borderColor = [UIColor greenColor].CGColor;
    self.scanAreaView.layer.borderWidth = 1.0f;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.scanView stop];
}


- (void)didReceiveMemoryWarning
{
       // Dispose of any resources that can be recreated.
}

//-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
//{
//    CGFloat x,y,width,height;
//    
//    x = rect.origin.x / readerViewBounds.size.width;
//    y = rect.origin.y / readerViewBounds.size.height;
//    width = rect.size.width / readerViewBounds.size.width;
//    height = rect.size.height / readerViewBounds.size.height;
//    
//    return CGRectMake(x, y, width, height);
//}

- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    
    for (ZBarSymbol *symbol in symbols) {
        NSLog(@"%@", symbol.data);
            NSString *resultString = [NSString stringWithFormat:@"%@",symbol.data];
            [self.navigationController popViewControllerAnimated:YES];
            [self.delegate scanFinishedWithResult:resultString];
        break;
    }
    
    
//    [readerView stop];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



//-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
//{
//    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
//    ZBarSymbol * symbol;
//    for(symbol in results)
//        break;
//    
////    _readerView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//    
//    [picker dismissViewControllerAnimated:YES completion:nil];
//    
////    _label.text = symbol.data;
//}
@end
